//
//  CommentCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/24/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class CommentCell: UITableViewCell {
    
    private lazy var personLbl: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .rgb(60,72,88)
        lbl.font = .systemFont(ofSize: 17)
        return lbl
    }()
    
    private lazy var dateLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 13, weight: .medium)
        lbl.textColor = .greyColor
        lbl.textAlignment = .right
        return lbl
    }()
    
    private lazy var messageView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightBlueColor
        view.layer.cornerRadius = 5
        return  view
    }()
    
    private lazy var messageText: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .rgb(60,72,88)
        lbl.font = .systemFont(ofSize: 16)
        lbl.numberOfLines = 0
        return lbl
    }()
    
    private lazy var infoButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "infoButton"), for: .normal)
        return button
    }()
    lazy var replyButton: UIButton = {
       let button = UIButton()
       button.setImage(UIImage(named: "reply"), for: .normal)
       return button
    }()
    
    
    public var comment: AdvertComment! {
        didSet {
            personLbl.text = comment.profile
            messageText.text = comment.text
            
            // TODO: CHANGE TO NAME
            let parentName = comment.parent != nil ? comment.parent!.profile : nil
            
            messageText.attributedText = self.createMessage(isReply: comment.parent != nil, person: parentName, text: comment.text)
            
            self.getTimeText()
        }
    }
    
    
    private func createMessage (isReply: Bool, person: String?, text: String) -> NSAttributedString {
        let linkAttrs = [NSAttributedString.Key.foregroundColor : UIColor.rgb(47, 130, 210)]
        let messageAttrs = [NSAttributedString.Key.foregroundColor : UIColor.rgb(60, 72, 88)]
        
        if isReply {
            let linkText = NSMutableAttributedString(string: person! + ", ", attributes: linkAttrs)
            let messageText = NSMutableAttributedString(string: text, attributes: messageAttrs)
            linkText.append(messageText)
            
            return linkText
        } else {
            let messageText = NSMutableAttributedString(string: text, attributes: messageAttrs)
            return messageText
        }
    }
    
    private func getTimeText () {
        let formatterDate = FormatterDate()
        if let date = formatterDate.getDate(inputDate: comment.created!) {
            let time = formatterDate.getDateString(date: date, at: "dd/HH:mm")
            let components = time.components(separatedBy: "/")
            let day = components[0]
            let hourMinute = components[1]
            
            var monthText: String = ""
             
            if date.month.count > 3 {
                monthText = date.month[...date.month.index(date.month.startIndex, offsetBy: 2)] + "."
            } else {
                monthText = date.month
            }
            self.dateLbl.text = "\(day) \(monthText)/ \(hourMinute)"
        } else {
            self.dateLbl.text = ""
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension CommentCell {
    
    private func addViews() {
        contentView.addSubview(personLbl)
        contentView.addSubview(dateLbl)
        contentView.addSubview(messageView)
        messageView.addSubview(messageText)
        contentView.addSubview(infoButton)
        contentView.addSubview(replyButton)
        
        self.addConstraints()
    }
    
    private func addConstraints () {
        dateLbl.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(12)
            make.trailing.equalToSuperview().inset(16)
            make.height.equalTo(20)
        }
        personLbl.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(12)
            make.leading.equalToSuperview().inset(16)
            make.trailing.equalTo(dateLbl.snp.leading).offset(-8)
            make.height.equalTo(20)
        }
        messageView.snp.makeConstraints { (make) in
            make.top.equalTo(personLbl.snp.bottom).offset(8)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        messageText.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.bottom.equalToSuperview().inset(4)
        }
        infoButton.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().inset(16)
            make.width.height.equalTo(16)
            make.top.equalTo(messageView.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-12)
        }
        replyButton.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().inset(16)
            make.width.height.equalTo(16)
            make.top.equalTo(messageView.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
}
